package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.utils.ExceptionsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("affix")
@Log4j2
@Api(tags = "附件模块")
@CrossOrigin
public class AffixController {

    @Autowired
    AffixService affixService;

    @PostMapping("upload/{businessType}")
    @ApiOperation(value = "文件上传",notes = "文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "file", value = "文件对象", required = true, dataType = "__file"),
            @ApiImplicitParam(paramType = "path", name = "businessType",value = "业务类型",dataType = "String")

    })
    public ResponseWrap<AffixVo> upLoad(
        MultipartFile file,
        @PathVariable("businessType") String businessType
    ) throws ProjectException {
        AffixVo affixVo = AffixVo.builder().businessType(businessType).build();
        AffixVo affixVoResult = null;
        try {
            affixVoResult = affixService.upload(file, affixVo);
            return ResponseWrap.<AffixVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(affixVoResult)
                    .build();
        } catch (IOException e) {
            log.error("文件上传异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.UPLOAD_FAIL.getCode(),
                    StatusEnum.UPLOAD_FAIL.getMsg()
            );
        }
    }

    @PostMapping("bindBusinessId")
    @ApiOperation(value = "附件绑定业务",notes = "附件绑定业务")
    @ApiImplicitParam(name = "affixVo",value = "附件对象",required = true,dataType = "AffixVo")
    public ResponseWrap<Boolean> bindBusinessId(@RequestBody AffixVo affixVo) throws ProjectException {
        try {
            Boolean flag = affixService.bindBusinessId(affixVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        } catch (Exception e) {
            log.error("附件绑定业务异常：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.BIND_BUSINESS_FAIL.getCode(),
                    StatusEnum.BIND_BUSINESS_FAIL.getMsg()
            );
        }
    }

    @GetMapping("{businessId}")
    @ApiOperation(value = "查询业务对应附件",notes = "查询业务对应附件")
    @ApiImplicitParam(paramType = "path",name = "businessId",value = "附件对象",dataType = "Long")
    public ResponseWrap<List<AffixVo>> findAffixByBusinessId(
            @PathVariable("businessId")Long businessId
    ) throws ProjectException {
        AffixVo affixVo = AffixVo.builder().businessId(businessId).build();
        try {
            List<AffixVo> affixVos = affixService.findAffixByBusinessId(affixVo);
            return ResponseWrap.<List<AffixVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(affixVos)
                    .build();
        } catch (Exception e) {
            log.error("查询业务对应附件：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.FIND_AFFIX_BUSINESSID_FAIL.getCode(),
                    StatusEnum.FIND_AFFIX_BUSINESSID_FAIL.getMsg()
            );
        }
    }


}
